﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace liaowa2.Utils.Cookie
{
    public class CookieHelper
    {
        #region cookie操作
        /// <summary>
        /// 清除指定Cookie
        /// </summary>
        /// <param name="cookiename">cookiename</param>
        public static void ClearCookie(string cookiename)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddYears(-3);
                cookie.Value = string.Empty;
                //cookie.Domain = UtilsBase.GetAppValueByName("Domain");
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }

        /// <summary>
        /// 清除所有站点cookie
        /// </summary>
        public static void ClearCookie()
        {
            var cookies = HttpContext.Current.Request.Cookies.AllKeys;
            foreach (var item in cookies)
            {
                SetCookie(item, string.Empty, -10);
            }
        }

        /// <summary>
        /// 获取指定Cookie值
        /// </summary>
        /// <param name="cookiename">cookiename</param>
        /// <returns></returns>
        public static string GetCookieValue(string cookiename)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
            string str = string.Empty;
            if (cookie != null)
            {
                str = HttpUtility.UrlDecode(cookie.Value);
            }
            return str;
        }

        /// <summary>
        /// 添加一个Cookie
        /// </summary>
        /// <param name="cookiename">cookie名</param>
        /// <param name="cookievalue">cookie值</param>
        /// <param name="expDay">几天后过期</param>
        public static void SetCookie(string cookiename, string cookievalue, double expDay)
        {
            HttpCookie cookie = new HttpCookie(cookiename)
            {
                Value = HttpUtility.UrlEncode(cookievalue),
                Expires = DateTime.Now.AddDays(expDay),
                Domain = UtilsBase.GetAppValueByName("Domain")
            };
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        #endregion

        /// <summary>
        /// 解码页面传来的encodeURIComponent编码字符串
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string DecodeURI(string html)
        {
            return HttpUtility.UrlDecode(html);
        }

        /// <summary>
        /// 添加一个Cookie（7*24小时过期）
        /// </summary>
        /// <param name="cookiename"></param>
        /// <param name="cookievalue"></param>
        public static void SetCookie(string cookiename, string cookievalue)
        {
            //cookie 设置为一年 2013-12-05
            SetCookie(cookiename, cookievalue, DateTime.Now.AddYears(1));
        }

        /// <summary>
        /// 添加一个Cookie
        /// </summary>
        /// <param name="cookiename">cookie名</param>
        /// <param name="cookievalue">cookie值</param>
        /// <param name="expires">过期时间 DateTime</param>
        public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
        {
            HttpCookie cookie = new HttpCookie(cookiename)
            {
                Value = HttpUtility.UrlEncode(cookievalue),
                Expires = expires,
                Domain = UtilsBase.GetAppValueByName("Domain")
            };
            //cookie.Domain = "fao.com";
            HttpContext.Current.Response.Cookies.Add(cookie);
        }


        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="strValue">过期时间(分钟)</param>
        public static void WriteCookieByDay(string strName, string strValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = UtilsBase.UrlEncode(strValue);
            cookie.Expires = DateTime.Now.AddDays(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

    }
}
